package com.glory.huaweipda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import androidx.databinding.DataBindingUtil
import com.glory.huaweipda.R
import com.glory.huaweipda.base.MyApp
import com.glory.huaweipda.bean.LotInfoBean
import com.glory.huaweipda.bean.SpcitemByWafer
import com.glory.huaweipda.databinding.ActivityTrackoutBinding
import com.glory.huaweipda.viewmodel.TrackOutModel
import com.glory.mvvmnet.base.BaseActivity
import kotlinx.android.synthetic.main.activity_trackout.*


class TrackOutActivity : BaseActivity<TrackOutModel>() {


    var eqpID: String = ""
    var user: String = ""
    var lotID: String = ""
    var type: String = ""
    var spcList = ArrayList<SpcitemByWafer>()

    lateinit var lotInfoBean: LotInfoBean
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                0x10 -> {
                    arrival.setEnabled(true)
                    var s: String = msg.obj as String
                    type = "oo"
                    val intent = Intent()
                    intent.putExtra("showPlugin", "errorLog")
                    intent.putExtra("error", s)
                    intent.putExtra("returncode", "1")
                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
                    intent.setClass(this@TrackOutActivity, clazz)
                    startActivity(intent)
                }
            }
        }
    }


    override fun layoutId(): Int {
        return R.layout.activity_trackout
    }

    override fun initView(savedInstanceState: Bundle?) {
        val instance = MyApp.instance
        instance!!.handler = handler

        val bind: ActivityTrackoutBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_trackout)

        bind.trackOutModel = viewModel
        eqpID = intent.getStringExtra("eqpID").toString()
        user = intent.getStringExtra("user").toString()
        lotID = intent.getStringExtra("lotID").toString()
        title_text.text = "结束作业"
        equipment_code.text = eqpID
        batch_code.setText(lotID)

        lotInfoBean = intent.getSerializableExtra("lotsBean") as LotInfoBean
        val lotsBean = lotInfoBean.lots[0]
        eqpState.setText(lotsBean.eqpState)
        recipeName.setText(lotsBean.recipeName)
        recipeDesc.setText(lotsBean.recipeDesc)
        stepName.setText(lotsBean.stepName)
        priority.setText(lotsBean.priority)

        if (lotInfoBean.lots.isNotEmpty()) {
//                for (w in lotsBean.waferList.wafer.indices){
//                    val sbw = SpcitemByWafer()
//                    sbw.waferDataName = lotsBean.waferList.wafer[w].waferid
//                    sbw.waferDataValue = lotsBean.waferList.wafer[w].slotno
//                    spcList.add(sbw)
//                }
            if (lotsBean.spclist!=null&&lotsBean.spclist.spcitemByWafer!=null&&lotsBean.spclist.spcitemByWafer.isNotEmpty()) {
                for (s in lotsBean.spclist.spcitemByWafer.indices) {
                    val sbw = SpcitemByWafer()
                    sbw.waferDataName = lotsBean.spclist.spcitemByWafer[s].waferDataName
                    sbw.waferDataValue = lotsBean.spclist.spcitemByWafer[s].waferDataValue
                    spcList.add(sbw)
                }
                viewModel.mfgItems.addAll(spcList)
            }
        }
        nextStepName.setText(lotsBean.nextStepName)
//        viewModel.defaultLotInfo(eqpID, user, lotID)


        arrival.setOnClickListener {
            arrival.setEnabled(false)
            viewModel.defaultTrackOut(eqpID, user, lotID)
        }


        again.setOnClickListener {
            val intent = Intent()
            intent.putExtra("showPlugin", "again")
            val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
            intent.setClass(this, clazz)
            startActivityForResult(intent, 1)
        }

        ivBack.setOnClickListener {
            finish()
        }

        viewModel.defUI.callObserve.observe(this) {
            if (type.equals("oo")) {
                it.type = 3
            }
            when (it.type) {
                0 -> {

                }
                1 -> {
                    finish()
                }
                200 -> {
//                    val intent = Intent()
//                    intent.putExtra("showPlugin", "arrival")
//                    intent.putExtra("track", "track")
//                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
//                    intent.setClass(this, clazz)
//                    intent.putExtra("track", "track")
//                    startActivityForResult(intent, 1)
                }
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                1 -> {
                    val extras = data!!.getExtras()
                    val again = extras!!.getString("again")
                    viewModel.defaultCancelTrackIn(eqpID, user, lotID, again.toString())
                }
                2 -> {
                    Log.e("onActivityResult","onActivityResult----------->")
                    val extras = data!!.getExtras()
                    val again = extras!!.getString("again")
                    finish()
                }

            }

        }

    }

    override fun initData() {

    }

}